package com.numberONe.controller.patientcase;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.GsonFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.numberONe.controller.index.BaseController;
import com.numberONe.entity.CaseFormMap;
import com.numberONe.entity.ConstitutionFormMap;
import com.numberONe.entity.PatientConstitutionResultFormMap;
import com.numberONe.entity.PatientFormMap;
import com.numberONe.entity.PatientPhysicalResultMap;
import com.numberONe.entity.PhysicalResult;
import com.numberONe.entity.UserFormMap;
import com.numberONe.mapper.ConstitutionMapper;
import com.numberONe.mapper.PatientConstitutionResultMapper;
import com.numberONe.mapper.PatientMapper;
import com.numberONe.util.Common;

/**
 * 体质分型controller
 * @author zy   
 * @version 100
 */
@Controller
@RequestMapping("/physical/")
public class PhysicalPartingController extends BaseController {
	

	
	@Inject
	PatientConstitutionResultMapper patientConstitutionResultMapper;
	
	@Autowired
	private ConstitutionMapper constitutionMapper;
	@Autowired
	private PatientMapper patientMapper;
	
	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		return "/physical";
	}
	
	@RequestMapping(value = "/create")
	public String create(Model model, HttpSession session,HttpServletRequest req,
			@RequestParam(value = "patient_number") 	String patient_number,
			@RequestParam(value = "pinghe") 	String pinghe,
			@RequestParam(value = "yangxu") 	String yangxu,
			@RequestParam(value = "yinxu") 		String yinxu,
			@RequestParam(value = "qixu") 		String qixu,
			@RequestParam(value = "tanshi") 	String tanshi,
			@RequestParam(value = "shire") 		String shire,
			@RequestParam(value = "xieyu") 		String xieyu,
			@RequestParam(value = "tebing") 	String tebing,
			@RequestParam(value = "qiyu") 		String qiyu) throws Exception{
		System.out.println("patient_number:"+patient_number);
		System.out.println("pinghe:"+pinghe);
		System.out.println("yangxu:"+yangxu);
		System.out.println("yinxu:"+yinxu);
		System.out.println("qixu:"+qixu);
		System.out.println("tanshi:"+tanshi);
		System.out.println("shire:"+shire);
		System.out.println("xieyu:"+xieyu);
		System.out.println("tebing:"+tebing);
		System.out.println("qiyu:"+qiyu);
		Map<String,Integer>	pcsMap = new HashMap<String,Integer>();
		pcsMap.put("平和质", castScore(pinghe));
		pcsMap.put("阳虚质", castScore(yangxu));
		pcsMap.put("阴虚质", castScore(yinxu));
		pcsMap.put("气虚质", castScore(qixu));
		pcsMap.put("痰湿质", castScore(tanshi));
		pcsMap.put("湿热质", castScore(shire));
		pcsMap.put("血瘀质", castScore(xieyu));
		pcsMap.put("特禀质", castScore(tebing));
		pcsMap.put("气郁质", castScore(qiyu));
		
//		pcsMap.put("pinghe", 59);
//		pcsMap.put("yangxu", 35);
//		pcsMap.put("yinxu", 29);
//		pcsMap.put("qixu", 29);
//		pcsMap.put("tanshi", 35);
//		pcsMap.put("shire", 29);
//		pcsMap.put("xieyu", 29);
//		pcsMap.put("tebing", 29);
//		pcsMap.put("qiyu", 29);
		//TODO 存入患者体质分数表patient_constitution_scores
		
		Map<String,PhysicalResult> resultMap = new HashMap<String,PhysicalResult>();
		UserFormMap userFormMap = (UserFormMap)session.getAttribute("userSession");
		PatientPhysicalResultMap patientResultMap = new PatientPhysicalResultMap();
		PatientFormMap patient = new PatientFormMap();
		if (Common.isNotEmpty(patient_number)) {
			patient= patientMapper.findbyFrist("patient_number", patient_number, PatientFormMap.class);
		}
		
		int pingheS = 	pcsMap.get("平和质");	//平和质转化分
		int yangxuS = 	pcsMap.get("阳虚质");	//阳虚质转化分
		int yinxuS 	=	pcsMap.get("阴虚质");		//阴虚质转化分
		int qixuS 	= 	pcsMap.get("气虚质");		//气虚质转化分
		int tanshiS = 	pcsMap.get("痰湿质");	//痰湿质转化分
		int shireS 	= 	pcsMap.get("湿热质");		//湿热质转化分
		int xieyuS 	= 	pcsMap.get("血瘀质");		//血瘀质转化分
		int tebingS = 	pcsMap.get("特禀质");	//特禀质转化分
		int qiyuS 	= 	pcsMap.get("气郁质");		//气郁质转化分
		
		//判断所属体质
		if(pingheS >= 60 
				&& yangxuS < 30
				&& yinxuS < 30
				&& qixuS < 30
				&& tanshiS < 30
				&& shireS < 30
				&& xieyuS < 30
				&& tebingS < 30
				&& qiyuS < 30){
			PhysicalResult physicalResult = new PhysicalResult();
			physicalResult.setScore(pingheS);
			physicalResult.setResultName("平和体质");
			physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "平和质", ConstitutionFormMap.class).getStr("constitution_desciption"));
			resultMap.put("平和质",physicalResult);
		}else if(pingheS >= 60 
				&& yangxuS < 40
				&& yinxuS < 40
				&& qixuS < 40
				&& tanshiS < 40
				&& shireS < 40
				&& xieyuS < 40
				&& tebingS < 40
				&& qiyuS < 40){
			PhysicalResult physicalResult = new PhysicalResult();
			physicalResult.setScore(pingheS);
			physicalResult.setResultName("基本是平和体质");
			physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "平和质", ConstitutionFormMap.class).getStr("constitution_desciption"));
			resultMap.put("平和质",physicalResult);
		} else {
			if(yangxuS >= 40){
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(yangxuS);
				physicalResult.setResultName("阳虚体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "阳虚质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("阳虚质",physicalResult);
			} else if((yangxuS>=30)&&(yangxuS<=39)) {
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(yangxuS);
				physicalResult.setResultName("基本是阳虚体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "阳虚质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("阳虚质",physicalResult);
			}
			if(yinxuS >= 40){
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(yinxuS);
				physicalResult.setResultName("阴虚体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "阴虚质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("阴虚质",physicalResult);
			} else if((yinxuS>=30)&&(yinxuS<=39)) {
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(yinxuS);
				physicalResult.setResultName("基本是阴虚体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "阴虚质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("阴虚质",physicalResult);
			}
			if(qixuS >= 40){
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(qixuS);
				physicalResult.setResultName("基本是气虚体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "气虚质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("气虚质",physicalResult);
			} else if((qixuS>=30)&&(qixuS<=39)) {
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(qixuS);
				physicalResult.setResultName("气虚体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "气虚质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("气虚质",physicalResult);
			}
			if(tanshiS >= 40){
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(tanshiS);
				physicalResult.setResultName("痰湿体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "痰湿质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("痰湿质",physicalResult);
			} else if((tanshiS>=30)&&(tanshiS<=39)) {
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(tanshiS);
				physicalResult.setResultName("基本是痰湿体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "痰湿质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("痰湿质",physicalResult);
			}
			if(shireS >= 40){
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(shireS);
				physicalResult.setResultName("湿热体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "湿热质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("湿热质",physicalResult);
			} else if((shireS>=30)&&(shireS<=39)) {
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(shireS);
				physicalResult.setResultName("基本是湿热体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "湿热质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("湿热质",physicalResult);
			}
			if(xieyuS >= 40){
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(xieyuS);
				physicalResult.setResultName("血瘀体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "血瘀质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("血瘀质",physicalResult);
			} else if((xieyuS>=30)&&(xieyuS<=39)) {
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(xieyuS);
				physicalResult.setResultName("基本是血瘀体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "血瘀质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("血瘀质",physicalResult);
			}
			if(tebingS >= 40){
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(tebingS);
				physicalResult.setResultName("特禀体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "特禀质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("特禀质",physicalResult);
			} else if((tebingS>=30)&&(tebingS<=39)) {
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(tebingS);
				physicalResult.setResultName("基本是特禀体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "特禀质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("特禀质",physicalResult);
			}
			if(qiyuS >= 40){
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(qiyuS);
				physicalResult.setResultName("气郁体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "气郁质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("气郁质",physicalResult);
			} else if((qiyuS>=30)&&(qiyuS<=39)) {
				PhysicalResult physicalResult = new PhysicalResult();
				physicalResult.setScore(qiyuS);
				physicalResult.setResultName("基本是气郁体质");
				physicalResult.setTreatment(constitutionMapper.findbyFrist("constiution_name", "气郁质", ConstitutionFormMap.class).getStr("constitution_desciption"));
				resultMap.put("气郁质",physicalResult);
			}
		}
		//TODO 患者体质超过两条，俺分数从高到低排取前两条
//		if(consFormList.size()>2){
//			model.addAttribute("consFormList",  consFormList.subList(0, 2));
//		} else {
//			model.addAttribute("consFormList",  consFormList);
//		}
		List<PhysicalResult> resultList = new ArrayList<PhysicalResult>(resultMap.values());    
		
		//排序
		Collections.sort(resultList, new Comparator<PhysicalResult>() {   
		    public int compare(PhysicalResult o1, PhysicalResult o2) {      
		        return (o2.getScore()).compareTo(o1.getScore());
		    }
		}); 
		
		patientResultMap.put("user", userFormMap);
		patientResultMap.put("patient", patient);
		
		model.addAttribute("physicalResult",  resultList.get(0));
		req.setAttribute("resultMap",  new Gson().toJson(resultMap));
		model.addAttribute("patientResultMap", patientResultMap);
		req.setAttribute("pcsMap", new Gson().toJson(pcsMap));
		return Common.WEB_PATH + "/physical/create";
	}
	
	/**
	 * 跳转打印页面
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/print")
	public String print(Model model,HttpSession session,HttpServletRequest req) throws Exception {
		ConstitutionFormMap constitutionFormMap = getFormMap(ConstitutionFormMap.class);
		if(constitutionFormMap.getStr("type")!=null&&!"".equals(constitutionFormMap.getStr("type"))) {
			PatientConstitutionResultFormMap PatientConstitutionResultFormMap = new PatientConstitutionResultFormMap();
			PatientConstitutionResultFormMap = patientConstitutionResultMapper.findbyFrist("id", constitutionFormMap.getStr("id"), PatientConstitutionResultFormMap.class);
			constitutionFormMap.set("conclusion",PatientConstitutionResultFormMap.getStr("constitution_result_strings"));
			constitutionFormMap.set("treatment",PatientConstitutionResultFormMap.getStr("treatment"));
			PatientFormMap patient =  patientMapper.findbyFrist("id", PatientConstitutionResultFormMap.getInt("patient_id")+"", PatientFormMap.class);
			constitutionFormMap.set("patient_number",patient.get("patient_number"));
			constitutionFormMap.set("patient_name",patient.get("patient_name"));
			constitutionFormMap.set("patient_sex",patient.get("patient_sex"));
			constitutionFormMap.set("patient_age",patient.get("patient_age"));
			constitutionFormMap.set("patient_address",patient.get("patient_address"));
			constitutionFormMap.set("prescription_date",PatientConstitutionResultFormMap.get("create_time"));
		}else {
			PatientConstitutionResultFormMap PatientConstitutionResultFormMap = new PatientConstitutionResultFormMap();
			SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			constitutionFormMap.set("prescription_date", dt.format(new Date()));
			UserFormMap userFormMap = (UserFormMap)session.getAttribute("userSession");
			String patient_id = constitutionFormMap.getStr("patient_id");
			PatientConstitutionResultFormMap.set("patient_id", Integer.parseInt(patient_id));
			PatientConstitutionResultFormMap.set("oprator_id", userFormMap.getInt("id"));
			PatientConstitutionResultFormMap.set("create_time", dt.format(new Date()));
			PatientConstitutionResultFormMap.set("constitution_result_strings", constitutionFormMap.getStr("conclusion"));
			PatientConstitutionResultFormMap.set("treatment", constitutionFormMap.getStr("treatment"));
			patientConstitutionResultMapper.addEntity(PatientConstitutionResultFormMap);
		}
		model.addAttribute("constitutionFormMap", constitutionFormMap);
		//req.setAttribute("pcsMap", constitutionFormMap.getStr("data"));
		return Common.WEB_PATH + "/physical/print";
	}
	
	/**
	 * 得出每个体质下的转化分
	 * @param scores
	 * @return
	 */
	public static Integer castScore(String scores){
		int originSum = 0;	//原始分
		int castScore = 0;	//转化分
		String[] scoresArr= scores.split(",");
		for(int i=0; i<scoresArr.length; i++){
			originSum += Integer.parseInt(scoresArr[i]);
		}
		DecimalFormat df = new DecimalFormat("0.00");//格式化小数 
		String temp=df.format((float)(originSum-scoresArr.length)/(scoresArr.length*4));
		castScore = (int) (Float.parseFloat(temp)*100);
		
		return castScore;
	} 
	
			
}
